package com.cloud.drore.eboss.supplier.service.impl;

import com.cloud.drore.eboos.common.base.BaseServiceImpl;
import com.cloud.drore.eboos.common.base.MyMapper;
import com.cloud.drore.eboos.common.entity.supplier.ProductRelation;
import com.cloud.drore.eboos.common.entity.supplier.Ticket;
import com.cloud.drore.eboos.common.entity.supplier.TicketPrice;
import com.cloud.drore.eboos.common.util.LocalData;
import com.cloud.drore.eboss.operation.entity.po.DistributorInfoPo;
import com.cloud.drore.eboss.operation.entity.vo.DistributorInfoVo;
import com.cloud.drore.eboss.operation.mapper.DistributorMapper;
import com.cloud.drore.eboss.supplier.entity.CombinationProduct;
import com.cloud.drore.eboss.supplier.entity.CombinationProductPrice;
import com.cloud.drore.eboss.supplier.entity.DistributorProductAssociation;
import com.cloud.drore.eboss.supplier.mapper.DistributorProductAssociationMapper;
import com.cloud.drore.eboss.supplier.service.IDistributorProductAssociationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 浙江卓锐科技股份有限公司 版权所有  Copyright 2018<br>
 *
 * @Description: <br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/2/1 11:00 <br>
 * @Author: <a href="1451173193@qq.com">杨亮</a>
 */
@Service
public class DistributorProductAssociationServiceImpl  extends BaseServiceImpl implements IDistributorProductAssociationService {
   @Autowired
   DistributorProductAssociationMapper distributorProductAssociationMapper;
    @Autowired
    DistributorMapper distributorMapper;
    @Override
    public MyMapper getMapper() {
        return distributorProductAssociationMapper;
    }
    //新增门票后自动关联上所有分销商
    @Override
    public void setTicketToAllDistributor(ProductRelation productRelation, TicketPrice ticketPrice, Ticket ticketInfo){
        List<DistributorInfoVo> disList=distributorMapper.getDistributorListByParam(new DistributorInfoPo());
        if(disList==null||disList.size()==0)
            return;
       List<DistributorProductAssociation> list =disList.stream().map(dis->{
           DistributorProductAssociation distributorProductAssociation=new DistributorProductAssociation();
           distributorProductAssociation.setCreatedDate(new Date());
            distributorProductAssociation.setDistributorId(dis.getId());
            distributorProductAssociation.setMainId(productRelation.getId());
            distributorProductAssociation.setProductName(productRelation.getProductName());
            distributorProductAssociation.setSalesPrice(ticketPrice.getSettlementPrice());
            distributorProductAssociation.setSettlementPrice(ticketPrice.getSettlementPrice());
           distributorProductAssociation.setTermOfValidityStartTime(ticketInfo.getTermOfValidityStartTime());
           distributorProductAssociation.setTermOfValidityEndTime(ticketInfo.getTermOfValidityEndTime());
            return distributorProductAssociation;
        }).collect(Collectors.toList());
        distributorProductAssociationMapper.insertList(list);
    }
    //新增组合票后自动关联上所有分销商
    @Override
    public void setCombinationToAllDistributor(ProductRelation productRelation, BigDecimal totalPrice, CombinationProduct combinationProduct) {
        List<DistributorInfoVo> disList=distributorMapper.getDistributorListByParam(new DistributorInfoPo());
        if(disList==null||disList.size()==0||totalPrice.compareTo(BigDecimal.ZERO)!=1)
            return;
       //final BigDecimal salesPrice=prices.stream().map(item->item.getSalesPrice()).reduce(BigDecimal.ZERO,BigDecimal::add);
      // final BigDecimal settlementPrice=prices.stream().map(item->item.getSettlementPrice()).reduce(BigDecimal.ZERO,BigDecimal::add);
        List<DistributorProductAssociation> list =disList.stream().map(dis->{
            DistributorProductAssociation distributorProductAssociation=new DistributorProductAssociation();
            distributorProductAssociation.setCreatedDate(new Date());
            distributorProductAssociation.setDistributorId(dis.getId());
            distributorProductAssociation.setMainId(productRelation.getId());
            distributorProductAssociation.setProductName(productRelation.getProductName());
            distributorProductAssociation.setSalesPrice(totalPrice);
            distributorProductAssociation.setSettlementPrice(totalPrice);
            distributorProductAssociation.setTermOfValidityStartTime(combinationProduct.getTermOfValidityStartTime());
            distributorProductAssociation.setTermOfValidityEndTime(combinationProduct.getTermOfValidityEndTime());
            return distributorProductAssociation;
        }).collect(Collectors.toList());
        distributorProductAssociationMapper.insertList(list);
    }

    @Override
    public void updateCooperationSupplierPriceByProductId(Long productId, BigDecimal supplierPrice) {
        Map<String,Object> param=new HashMap<>();
        param.put("product_id",productId);
        param.put("supplier_price",supplierPrice);
        param.put("created_by",LocalData.getCurrentUser().getId());
        param.put("created_date",new Date());
        distributorProductAssociationMapper.updateCooperationSupplierPriceByParam(param);
    }
}
